package com.orange.adminapi.controller.merchant;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.BR;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.merchant.dto.MerchantDTO;
import com.orange.core.pojo.entity.merchant.vo.MerchantVO;
import com.orange.core.pojo.req.merchant.MerchantPageReq;
import com.orange.core.service.merchant.MerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>ClassName:MerchantController</p>
 * <p>Description: 社区服务管理-商家信息controller</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/22 15:21</p>
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/merchant")
public class MerchantController {

    private final MerchantService merchantService;

    /**
     * 商家信息分页查询
     * @param pageReqDto
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COMMUNITY_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<MerchantVO>> page(@RequestBody PageReqDto<MerchantPageReq> pageReqDto){
        Page<MerchantVO> page = merchantService.page(pageReqDto);
        return R.ok(page);
    }

    /**
     * 商家信息详情
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COMMUNITY_INFO.permission.identifier)")
    @GetMapping("/info/{id}")
    public R<MerchantVO> info(@PathVariable("id") int id){
        return R.ok(merchantService.info(id));
    }

    /**
     * 商家信息新增
     * @param merchantDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COMMUNITY_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated MerchantDTO merchantDTO){
        return BR.result(merchantService.save(merchantDTO));
    }

    /**
     * 商家信息修改
     * @param merchantDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COMMUNITY_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated MerchantDTO merchantDTO){
        return BR.result(merchantService.update(merchantDTO));
    }

    /**
     * 商家信息删除
     * @param ids
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COMMUNITY_DELETE.permission.identifier)")
    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@PathVariable("ids") Integer[] ids){
        return BR.result(merchantService.deleteMerchantByIds(ids));
    }
}
